﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using System.Text;
using System.Drawing.Design;
using System.IO;

namespace mytreeview
{
    [Serializable]
    public class TreeViewc : TreeView
    {
        public TreeViewc()
        {
        }
        [Bindable(true)]
        [Category("搜索")]
        [Description("资源(image/css/js)的客户端根目录")]
        public string ClientScriptPath
        {
            get
            {
                String s = (String)ViewState["ClientScriptPath"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["ClientScriptPath"] = value;
            }
        }
        protected override void RaisePostBackEvent(string eventArgument)
        {
            base.RaisePostBackEvent(eventArgument);
            TreeNode tnNode = new TreeNode();
            //  base.RaisePostBackEvent(eventArgument);
            string sa = eventArgument.ToString();
            /*   for (int i = 0; i < this.Nodes.Count; i++)
               {
                   if (this.Nodes[i].Value == sa.Substring(sa.IndexOf('=')+1))
                   {
                       tnNode = this.Nodes[i];
                   }

               }*/
            string df = sa.Substring(sa.IndexOf('=') + 1).Replace("\\", "/");
            tnNode = this.FindNode(df);
            // tnNode.ChildNodes.Clear();
            if (sa.StartsWith("copy"))
            {
                Oncopynode(new nodeEventArgs(tnNode));
            }
            if (sa.StartsWith("paste"))
            {
                Onpastenode(new nodeEventArgs(tnNode));
            }
            if (sa.StartsWith("cut"))
            {
                Oncutnode(new nodeEventArgs(tnNode));
            }
            if (sa.StartsWith("delete"))
            {
                Ondeleteenode(new nodeEventArgs(tnNode));
            }
        }
        public event EventHandler copynode;
        protected virtual void Oncopynode(EventArgs e)
        {
            if (copynode != null)
            {
                copynode(this, e);
                //  Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script language='javascript' >alert('dfsd');</script>");
            }
        }
        public event EventHandler pastenode;
        protected virtual void Onpastenode(EventArgs e)
        {
            if (pastenode != null)
            {
                pastenode(this, e);
                //    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script language='javascript' >alert('dfsd');</script>");
            }
        }
        public event EventHandler addnode;
        protected virtual void Onaddnode(EventArgs e)
        {
            if (addnode != null)
            {
                addnode(this, e);
                //    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script language='javascript' >alert('dfsd');</script>");
            }
        }
        public event EventHandler deleteenode;
        protected virtual void Ondeleteenode(EventArgs e)
        {
            if (deleteenode != null)
            {
                deleteenode(this, e);
                //   Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script language='javascript' >alert('dfsd');</script>");
            }
        }
        public event EventHandler cutnode;
        protected virtual void Oncutnode(EventArgs e)
        {
            if (cutnode != null)
            {
                cutnode(this, e);
                //  Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "<script language='javascript' >alert('dfsd');</script>");
            }
        }
        /* protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write("控件属性示例, 请从:设计器界面<=>代码视图界面 切换查看效果.");
        }*/
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
            //  this.RenderControl(output);
            output.AddAttribute(HtmlTextWriterAttribute.Id, "myMenu");
            output.AddAttribute(HtmlTextWriterAttribute.Class, "contextMenu");
            output.RenderBeginTag(HtmlTextWriterTag.Ul);

            output.AddAttribute(HtmlTextWriterAttribute.Class, "copy");
            output.RenderBeginTag(HtmlTextWriterTag.Li);
            string href2 = Page.ClientScript.GetPostBackClientHyperlink(this, "this.svr").Replace("'this.svr'", "this.svr");
            output.AddAttribute(HtmlTextWriterAttribute.Href, href2);
            output.RenderBeginTag(HtmlTextWriterTag.A);
            // output.Write("<a href=javascript:PostBackFromClient_" + this.ClientID + "();>editos</a>");
            output.Write("复制");
            output.RenderEndTag();
            output.RenderEndTag();
            output.AddAttribute(HtmlTextWriterAttribute.Class, "cut");
            output.RenderBeginTag(HtmlTextWriterTag.Li);
            string href23 = Page.ClientScript.GetPostBackClientHyperlink(this, "this.svr").Replace("'this.svr'", "this.svr");
            output.AddAttribute(HtmlTextWriterAttribute.Href, href23);
            output.RenderBeginTag(HtmlTextWriterTag.A);
            // output.Write("<a href=javascript:PostBackFromClient_" + this.ClientID + "();>editos</a>");
            output.Write("剪切");
            output.RenderEndTag();
            output.RenderEndTag();
            output.AddAttribute(HtmlTextWriterAttribute.Class, "paste");
            output.RenderBeginTag(HtmlTextWriterTag.Li);
            string href233 = Page.ClientScript.GetPostBackClientHyperlink(this, "this.svr").Replace("'this.svr'", "this.svr");
            output.AddAttribute(HtmlTextWriterAttribute.Href, href233);
            output.RenderBeginTag(HtmlTextWriterTag.A);
            // output.Write("<a href=javascript:PostBackFromClient_" + this.ClientID + "();>editos</a>");
            output.Write("粘贴");
            output.RenderEndTag();
            output.RenderEndTag();
            output.AddAttribute(HtmlTextWriterAttribute.Class, "delete");
            output.RenderBeginTag(HtmlTextWriterTag.Li);
            string href = Page.ClientScript.GetPostBackClientHyperlink(this, "this.svr").Replace("'this.svr'", "this.svr");
            output.AddAttribute(HtmlTextWriterAttribute.Href, href);
            output.RenderBeginTag(HtmlTextWriterTag.A);
            // output.Write("<a href=javascript:PostBackFromClient_" + this.ClientID + "();>editos</a>");
            output.Write("删除");
            output.RenderEndTag();
            output.RenderEndTag();
            output.AddAttribute(HtmlTextWriterAttribute.Class, "quit");
            output.RenderBeginTag(HtmlTextWriterTag.Li);
            //  string href3 ="javascript: $('myMenu').hide()";
            //  output.AddAttribute(HtmlTextWriterAttribute.Href, href3);
            //   output.RenderBeginTag(HtmlTextWriterTag.A);
            output.Write("<a href=\"javascript:a()\">取消</a>");
            //  output.Write("新建");
            //  output.RenderEndTag();
            output.RenderEndTag();
            output.RenderEndTag();
            /*           output.Write("控件属性示例, 请从:设计器界面<=>代码视图界面 切换查看效果.");*/
        }

        protected override void OnPreRender(EventArgs e)
        {
            string strJSPath = base.ResolveUrl(Path.Combine(this.ClientScriptPath, @"treejb.js"));
            if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "treejb"))
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "treejb",
                    "<script type='text/javascript' src='" + strJSPath + "'></script>", false);
            }
            base.OnPreRender(e);
        }

    }


}